/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "BuilderApplication.h"

namespace PQBuilder
{

	const QString BuilderApplication::OWNER_NAME="PQ3D";
	const QString BuilderApplication::APPLICATION_NAME="PQBuilder";
	const QString BuilderApplication::STATE_FILE_NAME="appstate.dat";
	
	BuilderApplication::BuilderApplication()
	{
		_application=0;
		_mainWindow=0;
	}

	BuilderApplication::~BuilderApplication()
	{
		
		if(_mainWindow!=NULL){
			QByteArray bytes=_mainWindow->saveState();
			QString workDir=_application->applicationDirPath();
			QFile file(workDir+"/"+STATE_FILE_NAME);

			if (file.open(QIODevice::WriteOnly)){
				file.write(bytes);
					
			}

			delete _mainWindow;
		}

		if(_application!=NULL){
			delete _application;
		}
	}


	BuilderApplication& BuilderApplication::getSingleton()
	{
		static BuilderApplication inst;
		return inst;
	}

	void BuilderApplication::init(int argc, char **argv)
	{
		_application=new QApplication(argc, argv);
		_application->setOrganizationName(OWNER_NAME);
		_application->setApplicationName(APPLICATION_NAME);
		QString workDir=_application->applicationDirPath();
		QDir::setCurrent(workDir);

		initSplashScreen();
		initLanguage();

		_mainWindow=new MainWindow();

		
		QFile file(workDir+"/"+STATE_FILE_NAME);
		if (file.open(QIODevice::ReadOnly)){
				QByteArray bytes=file.readAll();
				_mainWindow->restoreState(bytes);
		}
	
	}

	void BuilderApplication::initSplashScreen()
	{
		QPixmap pixmap(":/images/Gdk3dLogo.png");
		QSplashScreen *splash = new QSplashScreen(pixmap);
		splash->show();
	}

	void BuilderApplication::initLanguage()
	{
		QSettings settings;
		QString languageFile = settings.value("preferences/customLanguage").toString();

		QTranslator defaultTranslator;
		QTranslator pq3dTranslator;

		if(languageFile != "pq3d_en.qm"){
			QString lang = "../lang/qt_" + languageFile;
			lang.remove("pq3d_");

			QString lang2 = "../lang/" + languageFile;

			if(QFile::exists(lang) && QFile::exists(lang2))
			{
				if(defaultTranslator.load(lang))
				_application->installTranslator(&defaultTranslator);

				if(pq3dTranslator.load(lang2))
				_application->installTranslator(&pq3dTranslator);
			}else{
				if(defaultTranslator.load("qt_" + QLocale::system().name(),QLibraryInfo::location(QLibraryInfo::TranslationsPath))){
					_application->installTranslator(&defaultTranslator);
				}else if(defaultTranslator.load("../lang/qt_" + QLocale::system().name())){
					_application->installTranslator(&defaultTranslator);
				}

				if(pq3dTranslator.load("../lang/pq3d_" + QLocale::system().name())){
					_application->installTranslator(&pq3dTranslator);
				}
			}
		}
	}

	void BuilderApplication::initStyleSheet()
	{
		QFile file("");
		file.open(QFile::ReadOnly);
		QString styleSheet = QLatin1String(file.readAll());

		//_application->setStyleSheet(styleSheet);
	}

	void BuilderApplication::run()
	{
		_mainWindow->show();
		_application->exec();
	}

	void BuilderApplication::loadProject(QString name)
	{
		QString title=name+ " - "+ GBApplication.APPLICATION_NAME;
		_mainWindow->setWindowTitle(title);
		_mainWindow->createDefaultUI();

	}

}
